package com.hnkywl.sts.action.jsgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.jsgl.*;
import com.hnkywl.sts.entity.cggl.Fp;
import com.hnkywl.sts.entity.cggl.Jsd;
import com.hnkywl.sts.entity.cggl.Xsjsdmx;
import com.hnkywl.sts.entity.sys.Gg;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import com.hnkywl.sts.service.cggl.CgdService;
import com.hnkywl.sts.service.jsgl.CgjsdService;
import com.hnkywl.sts.service.jsgl.FpService;
import com.hnkywl.sts.service.jsgl.WljsdService;
import com.hnkywl.sts.service.jsgl.XsjsdService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.xsgl.XsdService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.jbpm.api.Execution;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletOutputStream;
import java.io.*;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-30
 * Time: 上午9:57
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/jsgl/fp")
public class FpAction extends SimpleActionSupport {
    private FpService fpService;
    private CgjsdService cgjsdService;
    private WljsdService wljsdService;
    private XsjsdService xsjsdService;
    private Long fpcgjsdId;
    private Long fpwljsdId;
    private Long fpxsjsdId;
    private Jsd cgjsdFp;
    private Jsd wljsdFp;
    private Jsd xsjsdFp;
    private FpDao fpDao;
    private CgjsdmxDao cgjsdmxDao;
    private XsjsdmxDao xsjsdmxDao;
    private WljsdmxDao wljsdmxDao;
    private CgdService cgdService;
    private XsdService xsdService;
    private Date CurrentTime = new Date();
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //行索引
    private int index;
    //传入的企业机构Id值
    private Integer type;
    //结算单号
    private String jsdh;
    //客户的ID
    private Long glqyId;
    //发票类型ID
    private Long fpzlId;
    //客户的名称
    private String glqyQc;
    //公司的名称
    private String gsJc;
    //申请（查询）开始时间
    private Date sqrqStart;
    //申请（查询）结束时间
    private Date sqrqEnd;
    //销售结算单实体
    private Jsd fp;
    //销售结算单的ID
    private Long fpId;
    //申报人的Id
    private Long sprId;
    //申报人所在公司的Id
    private Long gsId;
    //状态
    private Integer shzt;
    //退回原因
    private String thyy;

    //发票张数
    private Long fpzs;
    //发票号码
    private String fphm;
    private Long xsjsdId;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    private File result = new File("result.txt");
    private FileWriter writer;

    private PrintWriter pw;

    private String fpze;

    private String kprq;

    //状态
    private Integer zt;

    private String fpIds;

    private File upload;


    //发票查询
    @Action("fpList")
    public void fpList() {
        Long ywgsID = YgService.getCurrentYgZgGsId();
        this.renderJson(this.fpService.findAllfp(type, zt, jsdh, sqrqStart, sqrqEnd, glqyId, glqyQc, gsId, gsJc, fpzlId, page, rows, ywgsID));
    }

    @Action("fp")
    public String fp() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //进入发票审核页面
    @Action("to_change_status")
    public String toChangeStatus() {
        fp = this.fpService.findfpDetail(fpId);
        return SUCCESS;
    }

    //发票审核
    @Action("change_status")
    public void changeStatus() {
        try {
            fpService.changeStatus(fpId, 2, fphm, fpzs, fpze, fp);
            renderText("{\"statusCode\":\"200\", \"message\":\"发票审核成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }


    //进入发票审核页面
    @Action("to_change_status_th")
    public String toChangeStatusTh() {
        fp = this.fpService.findfpDetail(fpId);
        return SUCCESS;
    }

    //发票退回
    @Action("change_status_th")
    public void changeStatusTh() {
        try {
            fpService.changeStatusTh(fpId, shzt, thyy);
            renderText("{\"statusCode\":\"200\", \"message\":\"发票退回成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //发票取消审核
    @Action("fp_qxsh")
    public void fpQxsh() {
        try {
            Jsd fp = fpService.findfpDetail(fpId);
            if (this.checkFpqxsh(fp.getKprq())) {
                if (fpDao.qxshyz(fpId)) {
                    fpService.FpQxsh(fpId);
                    renderText("发票已取消审核!");
                }
            } else {
                renderText("发票取消审核时间不可以跨月!");
            }

        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }

    // 导出金税格式
    @Action("fp_dcjsgs")
    public void fpDcjsgs() {
        try {
            String ss = "~~";
            String hc = "\r\n";//回车换行
            SimpleDateFormat smp = new SimpleDateFormat("yyyyMMddhhmmss");
            SimpleDateFormat smp1 = new SimpleDateFormat("yyyyMMdd");
            String remark = smp1.format(new Date()) + "-销售业务";
            StringBuilder sb = new StringBuilder();
            sb.append("SJJK0101" + ss + "销售单据传入" + ss + remark + hc);//第一行数据
            if (StringUtils.isNotBlank(fpIds)) {
                String[] fpid = fpIds.split(",");
                for (int i = 0; i < fpid.length; i++) {
                    if (StringUtils.isNotBlank(fpid[i])) {
                        fp = fpService.findfpDetail(Long.valueOf(fpid[i]));
                        if (null != fp && null != fp.getZt() && fp.getZt() == 2) {
                            sb.append("//单据" + (i + 1) + hc);
                            sb.append(fp.getJsdh() + ss + fp.getXsdmxList().size() + ss);
                            sb.append(fp.getGlqy().getQc() + ss + fp.getGlqy().getQyfr().getNsh() + ss);
                            sb.append(fp.getGlqy().getQyfr().getAddress() + fp.getGlqy().getQyfr().getDh() + ss);
                            sb.append(fp.getGlqy().getQyfr().getKhh() + fp.getGlqy().getQyfr().getZh() + ss + "已收款" + hc);
                            List<Map> xsdmxList = this.xsjsdmxDao.findXsjsdmxSearch(Long.valueOf(fpid[i]));
                            // 没有明细不能导入
                            if (null != xsdmxList && xsdmxList.size() > 0) {
                                for (Map map : xsdmxList) {
                                    Xsd xsd = this.xsdService.findXsdDetail(Long.valueOf(map.get("xsd.id").toString()));
                                    sb.append(map.get("wzgg.name") + ss + map.get("wzgg.dw") + ss + map.get("wzgg.ggxh") + ss);
                                    sb.append(map.get("ck_sbsl") + ss + map.get("se") + ss + xsd.getXsht().getSl() / 100.0 + ss);
                                    sb.append(1510 + ss + 0 + ss + map.get("fpse") + ss + 0 + ss + 0 + ss + map.get("kpbhsj") + ss + 0 + hc);
                                }
                                fpService.changeStatusTh(Long.valueOf(fpid[i]), 4, "");
                            }
                        }
                    }
                }
            }
            response.setContentType("application/x-msdownload");
            String filedisplay = new String((smp.format(new Date()) + ".txt").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            response.getOutputStream().print(new String((sb.toString()).getBytes("gb2312"), "iso8859-1"));
            ServletOutputStream sos = response.getOutputStream();
            StringWriter sw = new StringWriter();
            sw.write(sb.toString());
            sos.close();
            sw.close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("金税导出错误，请联系管理员！");
        }
    }

    // 发票冲销（作废）
    @Action("fptz")
    public void fptz() {
        try {
            String ss = "~~";
            String hc = "\r\n";//回车换行
            SimpleDateFormat smp = new SimpleDateFormat("yyyyMMddhhmmss");
            SimpleDateFormat smp1 = new SimpleDateFormat("yyyyMMdd");
            String remark = smp1.format(new Date()) + "-误传作废";
            String[] fpid = fpIds.split(",");
            StringBuilder sb = new StringBuilder();
            sb.append("SJJK0102" + ss + "作废销售单据传入" + ss + remark + hc);//第一行数据
            for (int i = 0; i < fpid.length; i++) {
                if (StringUtils.isNotBlank(fpid[i])) {
                    fp = fpService.findfpDetail(Long.valueOf(fpid[i]));
                    if (null != fp.getZt() && fp.getZt() == 4) {
                        sb.append("//单据" + (i + 1) + hc);
                        sb.append(fp.getJsdh() + hc);
                    }
                    // 改变状态为抛账
                    fpService.changeStatusTh(Long.valueOf(fpid[i]), 2, "");
                }
            }
            response.setContentType("application/x-msdownload");
            String filedisplay = new String((smp.format(new Date()) + ".txt").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            response.getOutputStream().print(new String((sb.toString()).getBytes("gb2312"), "iso8859-1"));
            ServletOutputStream sos = response.getOutputStream();
            StringWriter sw = new StringWriter();
            sw.write(sb.toString());
            sos.close();
            sw.close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("发票作废导出错误，请联系管理员！");
        }
    }

    /*@Action("fptz")
    public void fptz() {
        try {
            Jsd jsd = this.fpService.findfpDetail(fpId);
            if (jsd.getType() == 0) {
                this.cgjsdService.cgjsdtz(jsd);
            } else if (jsd.getType() == 1) {
                this.xsjsdService.xsjsdtz(jsd);
            } else {
                this.wljsdService.wljsdtz(jsd);
            }
            renderText("{\"statusCode\":\"200\", \"message\":\"发票冲销成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }*/

    // 金税格式导入
    @Action("fp_drjsgs")
    public void fpDrjsgs() {
        String encoding = "UTF-8";
        try {
            InputStreamReader read = new InputStreamReader(new FileInputStream(upload), encoding);//考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            List<String> list = new ArrayList<String>();
            while ((lineTxt = bufferedReader.readLine()) != null) {
                if (StringUtils.isNotBlank(lineTxt)) {
                    if (lineTxt.split("//").length > 1) {
                        list.add("flag");
                    } else {
                        list.add(lineTxt);
                    }
                }
            }
            read.close();
            List<Jsd> jsdList = new ArrayList<Jsd>();
            List<Xsjsdmx> xsjsdmxList = new ArrayList<Xsjsdmx>();
            if (list.size() > 3) {
                String condition = list.get(0).split("~~")[0];
                String fpNum = list.get(1).split("~~")[0]; // 发票张数
                String startDate = list.get(1).split("~~")[1]; // 开票起始开票日（日期）
                String endDate = list.get(1).split("~~")[2]; // 截止开票日（日期）
                if (StringUtils.equals(condition, "SJJK0201")) {
                    for (int i = 2; i < list.size(); i++) {
                        if (list.get(i).equals("flag")) {
                            // 处理单据主体
                            int k = i + 1;
                            Jsd dJsd = dealSubjectData(fpNum, startDate, endDate, list.get(k));
                            if (null != dJsd) {
                                jsdList.add(dJsd);
                            }

                            if (dJsd.getNum() > 0) {
                                // 处理单据明细
                                for (int j = k + 1; j < list.size(); j++) {
                                    if (list.get(j).equals("flag")) {
                                        break;
                                    } else {
                                        Xsjsdmx xsjsdmx = dealMxData(dJsd, list.get(j));
                                        if (null != xsjsdmx) {
                                            xsjsdmxList.add(xsjsdmx);
                                        }
                                    }
                                }
                            }
                        }
                    }
                } else {
                    renderText("{\"statusCode\":\"300\", \"message\":\"不是导入金税格式数据！\"}");
                }

                // 更新结果
                if (null != jsdList && jsdList.size() > 0 && null != xsjsdmxList && xsjsdmxList.size() > 0) {
                    this.xsjsdService.saveImportInfo(jsdList, xsjsdmxList);
                    renderText("{\"statusCode\":\"200\", \"message\":\"导入金税格式成功！\"}");
                } else {
                    renderText("{\"statusCode\":\"300\", \"message\":\"导入金税格式数据失败！\"}");
                }

            } else {
                renderText("{\"statusCode\":\"300\", \"message\":\"数据不完整无法导入！\"}");
            }

        } catch (UnsupportedEncodingException e) {
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        } catch (FileNotFoundException e) {
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        } catch (IOException e) {
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        } catch (Exception e) {
            renderText("{\"statusCode\":\"300\", \"message\":\"数据导入异常\"}");
        }
    }


    /**
     * 处理主体信息
     *
     * @param fpNum
     * @param startDate
     * @param endDate
     * @param subject   @return
     */
    private Jsd dealSubjectData(String fpNum, String startDate, String endDate, String subject) throws ParseException {
        if (subject.split("~~")[0].equals("0")) {
            List<Jsd> jsdList = xsjsdService.findXsjsdByJsdh(subject.split("~~")[8]);
            Jsd jsd = jsdList.get(0);
            if (jsd.getZt() == 4) {
                jsd.setFpzs(Long.valueOf(fpNum));
                jsd.setStartDate(startDate);
                jsd.setEndDate(endDate);
                jsd.setZt(3);
                jsd.setShzt(1);
                jsd.setFpzl(Long.valueOf(subject.split("~~")[2]));
                jsd.setFplb(subject.split("~~")[3]);
                jsd.setHm(subject.split("~~")[4]);
                jsd.setNum(Integer.valueOf(subject.split("~~")[5]));
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                jsd.setKprq(sdf.parse(subject.split("~~")[6]));
                jsd.setSwyf(subject.split("~~")[7]);
                jsd.setBhsje(Double.valueOf(subject.split("~~")[9]));
                Double swse = Double.valueOf(subject.split("~~")[11]);
                jsd.setSwse(swse);
                Double se = jsd.getSe();
                jsd.setCe(se - swse);
                jsd.setDrrq(new Date());
                return jsd;
            }

        }
        return null;
    }


    /**
     * 处理数据
     *
     * @param jsd
     * @param detail
     * @return
     */
    private Xsjsdmx dealMxData(Jsd jsd, String detail) {
        String name = detail.split("~~")[1];
        String ggxh = detail.split("~~")[2];
        Double sl = Double.valueOf(detail.split("~~")[4]);
        Double swje = Double.valueOf(detail.split("~~")[5]);
        Double swse = Double.valueOf(detail.split("~~")[7]);
        Double bhsj = Double.valueOf(detail.split("~~")[8]);
        Xsjsdmx xsjsdmx = this.xsjsdService.findXsjsdDetailByCondition(jsd.getXsjsdmxList(), name, ggxh, sl, bhsj);
        if (null != xsjsdmx) {
            xsjsdmx.setSwje(swje);
            xsjsdmx.setSwse(swse);
            xsjsdmx.setCe(xsjsdmx.getSe() - swse);
            return xsjsdmx;
        }
        return null;
    }

    public void finish() { //关闭输入流，将文字从缓存写入文件
        try {
            pw.flush();
            writer.close();
        } catch (IOException iox) {
            System.err.println(iox);
        }
    }

    public void aLine(String in) { //写入一行
        pw.println(in);

    }


    @Action("fp_cgjsd_detail")
    public String fpCgjsdDetail() {
        if (fpcgjsdId != null) {
            cgjsdFp = cgjsdService.findCgjsdDetail(fpcgjsdId);
        } else {
            cgjsdFp = new Jsd();
        }
        return SUCCESS;
    }

    //采购结算单中查询明细
    @Action("fp_cgdmxSearchList")
    public void fpCgdmxSearchList() {
        this.renderJson(this.cgdService.findCgdmxSearch(fpcgjsdId));
    }

    //采购结算单中表格
    @Action("fpcgjsdDetail")
    public void fpcgjsdDetail() {
        Gson gson = new Gson();
        List<Map> fpcgjsdList = new ArrayList<Map>();
        Jsd jsd = this.cgjsdService.findCgjsdDetail(fpcgjsdId);
        if (jsd.getShzt() == 1) {
            fpcgjsdList = this.cgjsdmxDao.findCgjsdmxSearch(fpcgjsdId);
        } else {
            fpcgjsdList = this.fpDao.fpCgjsdList(fpcgjsdId);
        }
        Double sbsl = 0d;
        Double hsj = 0d;
        Double bhsj = 0d;
        Double hse = 0d;
        Double bhse = 0d;
        Double je = 0d;
        Double se = 0d;
        String result = "";
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
        java.text.DecimalFormat jtdf = new java.text.DecimalFormat("0.0000");
        StringBuffer sbTable = new StringBuffer();
        if (null != fpcgjsdList) {
            sbTable.append("<table class=\"tb intb\">");
            sbTable.append("<tr>");
            sbTable.append("<td style='width:110px;'>采购单号</td><td width='60px'>品名</td><td width='45px'>材质</td>" +
                    " <td td width='45px'>规格型号</td><td width='40px'>产地</td>" +
                    "<td width='30px'>单位</td><td style='width:90px;'>数量</td><td style='width:70px;'>含税单价(元)</td><td style='width:80px;'>不含税单价(元)</td><td style='width:80px;'>含税总金额(元)</td><td style='width:80px;'>净余额(元)</td><td style='width:80px;'>税额(元)</td>");
            sbTable.append("</tr>");
            for (int i = 0; i < fpcgjsdList.size(); i++) {
                sbTable.append("<tr>");

                sbTable.append("<td>");
                if (null != fpcgjsdList.get(i).get("cgdh")) {
                    sbTable.append(fpcgjsdList.get(i).get("cgdh"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpcgjsdList.get(i).get("wzgg.name")) {
                    sbTable.append(fpcgjsdList.get(i).get("wzgg.name"));
                }
                sbTable.append("</td>");
                sbTable.append("<td>");
                if (null != fpcgjsdList.get(i).get("wzgg.cz")) {
                    sbTable.append(fpcgjsdList.get(i).get("wzgg.cz"));
                }
                sbTable.append("</td>");
                sbTable.append("<td>");
                if (null != fpcgjsdList.get(i).get("wzgg.ggxh")) {
                    sbTable.append(fpcgjsdList.get(i).get("wzgg.ggxh"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpcgjsdList.get(i).get("cd.z")) {
                    sbTable.append(fpcgjsdList.get(i).get("cd.z"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpcgjsdList.get(i).get("wzgg.dw")) {
                    sbTable.append(fpcgjsdList.get(i).get("wzgg.dw"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpcgjsdList.get(i).get("rksl")) {
                    sbTable.append(jtdf.format(fpcgjsdList.get(i).get("rksl")));
                    sbsl += (Double) fpcgjsdList.get(i).get("rksl");
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpcgjsdList.get(i).get("hsj")) {
                    sbTable.append(df.format(fpcgjsdList.get(i).get("hsj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpcgjsdList.get(i).get("bhsj")) {
                    sbTable.append(df.format(fpcgjsdList.get(i).get("bhsj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpcgjsdList.get(i).get("je") && null != fpcgjsdList.get(i).get("se")) {
                    Double fpmje = (Double) fpcgjsdList.get(i).get("je");
                    Double fpmse = (Double) fpcgjsdList.get(i).get("se");
                    Double fame = (Double) fpcgjsdList.get(i).get("me");
                    Double h_se = fpmje + fpmse;
                    sbTable.append(df.format(h_se));
                    hse += h_se;
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpcgjsdList.get(i).get("je")) {
                    sbTable.append(df.format(fpcgjsdList.get(i).get("je")));
                    je += Double.valueOf(fpcgjsdList.get(i).get("je").toString());
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpcgjsdList.get(i).get("se")) {
                    sbTable.append(df.format(fpcgjsdList.get(i).get("se")));
                    se += Double.valueOf(fpcgjsdList.get(i).get("se").toString());
                }
                sbTable.append("</td>");

                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th colspan=\"6\">合计：");
            sbTable.append("</th>");

            sbTable.append("<td>");
            sbTable.append(jtdf.format(sbsl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(hse));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(je));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(se));
            sbTable.append("</td>");

            sbTable.append("</tr>");
            sbTable.append("</table>");
        }
        renderHtml(sbTable.toString());
    }

    @Action("fp_xsjsd_detail")
    public String fp_xsjsd_detail() {
        if (fpxsjsdId != null) {
            xsjsdFp = xsjsdService.findXsjsdDetail(fpxsjsdId);
        } else {
            xsjsdFp = new Jsd();
        }
        return SUCCESS;
    }

    //销售结算单中查询明细
    @Action("fp_xsdmxSearchList")
    public void fpXsdmxSearchList() {
        this.renderJson(this.xsdService.findXsdmxSearchDetail(fpxsjsdId));
    }

    //销售结算单中表格
    @Action("fpxsjsdDetail")
    public void fpxsjsdDetail() {
        List<Map> fpxsjsdList;
        Jsd jsd = this.xsjsdService.findXsjsdDetail(fpxsjsdId);
        if (jsd.getShzt() == 1) {
            fpxsjsdList = this.xsjsdmxDao.findXsjsdmxSearch(fpxsjsdId);
        } else {
            fpxsjsdList = this.fpDao.fpXsjsdList(fpxsjsdId);
        }
        Double sbsl = 0d;
        Double hse = 0d;
        Double je = 0d;
        Double se = 0d;
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
        java.text.DecimalFormat jtdf = new java.text.DecimalFormat("0.0000");
        StringBuffer sbTable = new StringBuffer();
        if (null != fpxsjsdList) {
            sbTable.append("<table class=\"tb intb\">");
            sbTable.append("<tr>");
            sbTable.append("<td style='width:110px;'>销售合同子项号</td><td style='width:110px;'>销售单号</td><td width='60px'>品名</td><td width='30px'>材质</td>" +
                    "<td  width='30px'>规格型号</td><td width='40px'>产地</td><td width='30px'>单位</td><td style='width:70px;'>数量</td>" +
                    "<td style='width:80px;'>含税单价(元)</td><td style='width:80px;'>实际单价(元)</td><td style='width:80px;'>购货金额(元)</td>" +
                    "<td style='width:80px;'>开票不含税单价(元)</td><td style='width:80px;'>金额(元)</td><td style='width:80px;'>税额(元)</td>");
            sbTable.append("</tr>");
            for (int i = 0; i < fpxsjsdList.size(); i++) {
                sbTable.append("<tr>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("yyxsdh")) {
                    sbTable.append(fpxsjsdList.get(i).get("yyxsdh"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("xsdh")) {
                    sbTable.append(fpxsjsdList.get(i).get("xsdh"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("wzgg.name")) {
                    sbTable.append(fpxsjsdList.get(i).get("wzgg.name"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("wzgg.cz")) {
                    sbTable.append(fpxsjsdList.get(i).get("wzgg.cz"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("wzgg.ggxh")) {
                    sbTable.append(fpxsjsdList.get(i).get("wzgg.ggxh"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("cd.z")) {
                    sbTable.append(fpxsjsdList.get(i).get("cd.z"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("wzgg.dw")) {
                    sbTable.append(fpxsjsdList.get(i).get("wzgg.dw"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("ck_sbsl")) {
                    sbTable.append(jtdf.format(fpxsjsdList.get(i).get("ck_sbsl")));
                    sbsl += (Double) (fpxsjsdList.get(i).get("ck_sbsl"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("hsj")) {
                    sbTable.append(jtdf.format(fpxsjsdList.get(i).get("hsj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("sjdj")) {
                    sbTable.append(jtdf.format(fpxsjsdList.get(i).get("sjdj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("hsj") && null != fpxsjsdList.get(i).get("ck_sbsl")) {
                    // 判断是否已经导入金税
                    if (jsd.getShzt() == 1) {
                        Double mhse = (Double) fpxsjsdList.get(i).get("swje");
                        BigDecimal bigDecimal_total_mhse = new BigDecimal(mhse).setScale(2, BigDecimal.ROUND_HALF_UP);
                        sbTable.append(bigDecimal_total_mhse);
                        hse += mhse;
                    } else {
                        Double mhse = (Double) fpxsjsdList.get(i).get("se");
                        BigDecimal bigDecimal_total_mhse = new BigDecimal(mhse).setScale(2, BigDecimal.ROUND_HALF_UP);
                        sbTable.append(bigDecimal_total_mhse);
                        hse += mhse;
                    }

                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("kpbhsj")) {
                    sbTable.append(jtdf.format(fpxsjsdList.get(i).get("kpbhsj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("je")) {
                    // 判断是否已经导入金税
                    if (jsd.getShzt() == 1) {
                        Double swje = Double.valueOf(fpxsjsdList.get(i).get("swje").toString());
                        Double swse = Double.valueOf(fpxsjsdList.get(i).get("swse").toString());
                        Double mje = swje - swse;
                        BigDecimal bigDecimal_total_mje = new BigDecimal(mje).setScale(2, BigDecimal.ROUND_HALF_UP);
                        sbTable.append(bigDecimal_total_mje);
                        je += mje;
                    } else {
                        Double mje = Double.valueOf(fpxsjsdList.get(i).get("je").toString());
                        BigDecimal bigDecimal_total_mje = new BigDecimal(mje).setScale(2, BigDecimal.ROUND_HALF_UP);
                        sbTable.append(bigDecimal_total_mje);
                        je += mje;
                    }
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("se")) {
                    // 判断是否已经导入金税
                    if (jsd.getShzt() == 1) {
                        Double mse = Double.valueOf(fpxsjsdList.get(i).get("swse").toString());
                        BigDecimal bigDecimal_total_mse = new BigDecimal(mse).setScale(2, BigDecimal.ROUND_HALF_UP);
                        sbTable.append(bigDecimal_total_mse);
                        se += mse;
                    } else {
                        Double mse = Double.valueOf(fpxsjsdList.get(i).get("fpse").toString());
                        BigDecimal bigDecimal_total_mse = new BigDecimal(mse).setScale(2, BigDecimal.ROUND_HALF_UP);
                        sbTable.append(bigDecimal_total_mse);
                        se += mse;
                    }
                }
                sbTable.append("</td>");

                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th colspan=\"7\">合计：");
            sbTable.append("</th>");

            sbTable.append("<td>");
            sbTable.append(jtdf.format(sbsl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            BigDecimal bigDecimal_total_hse = new BigDecimal(hse).setScale(2, BigDecimal.ROUND_HALF_UP);
            sbTable.append(df.format(bigDecimal_total_hse));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            BigDecimal bigDecimal_total_je = new BigDecimal(je).setScale(2, BigDecimal.ROUND_HALF_UP);
            sbTable.append(df.format(bigDecimal_total_je));
            sbTable.append("</td>");

            sbTable.append("<td>");
            BigDecimal bigDecimal_total_se = new BigDecimal(se).setScale(2, BigDecimal.ROUND_HALF_UP);
            sbTable.append(df.format(bigDecimal_total_se));
            sbTable.append("</td>");

            sbTable.append("</tr>");
            sbTable.append("</table>");
        }
        renderHtml(sbTable.toString());
    }

    @Action("fp_wljsd_detail")
    public String fpWljsdDetail() {
        if (fpwljsdId != null) {
            wljsdFp = wljsdService.findWljsdDetail(fpwljsdId);
        } else {
            wljsdFp = new Jsd();
        }
        return SUCCESS;
    }


    //物流结算单中查询明细
    @Action("fp_wljsdmxSearchList")
    public void fpWljsdmxSearchList() {
        this.renderJson(this.wljsdService.findwldDetail(fpwljsdId));
    }

    //物流结算单中表格
    @Action("fpwljsdDetail")
    public void fpwljsdDetail() {
        Gson gson = new Gson();
        List<Map> fpwljsdList = new ArrayList<Map>();
        Jsd jsd = this.fpService.findfpDetail(fpwljsdId);
        if (jsd.getShzt() == 1) {
            fpwljsdList = this.wljsdmxDao.findWljsdDetail(fpwljsdId);
        } else
            fpwljsdList = this.fpDao.fpWljsdList(fpwljsdId);
        Double jfsl = 0d;
        Double je = 0d;
        Double hdsl = 0d;
        Double hdje = 0d;
        Double kdse = 0d;
        Double se = 0d;
        Double me = 0d;
        Double ce = 0d;
        Double jsje = 0d;

        String result = "";
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
        java.text.DecimalFormat df2 = new java.text.DecimalFormat("0.0000");
        StringBuffer sbTable = new StringBuffer();
        if (null != fpwljsdList) {
            sbTable.append("<table class=\"tb intb\">");
            sbTable.append("<tr>");
            sbTable.append("<td >费用类别</td><td>计费数量</td><td>单价(元)</td><td>金额(元)</td><td>核定数量</td>" +
                    "<td>核定单价(元)</td><td>核定金额(元)</td><td>面额(元)</td><td>可抵税额(元)</td><td>税额(元)</td><td>净额(元)</td><td>差额(元)</td><td>备注</td>");
            sbTable.append("</tr>");
            for (int i = 0; i < fpwljsdList.size(); i++) {
                sbTable.append("<tr>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("fylb.fykmjc")) {
                    sbTable.append(fpwljsdList.get(i).get("fylb.fykmjc"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("jfsl")) {
                    sbTable.append(df2.format(fpwljsdList.get(i).get("jfsl")));
                    jfsl += (Double) (fpwljsdList.get(i).get("jfsl"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("dj")) {
                    sbTable.append(df2.format(fpwljsdList.get(i).get("dj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("je")) {
                    sbTable.append(df.format(fpwljsdList.get(i).get("je")));
                    je += (Double) (fpwljsdList.get(i).get("je"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("hdsl")) {
                    sbTable.append(df2.format(fpwljsdList.get(i).get("hdsl")));
                    hdsl += (Double) (fpwljsdList.get(i).get("hdsl"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("hddj")) {
                    sbTable.append(df2.format(fpwljsdList.get(i).get("hddj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("hdje")) {
                    sbTable.append(df.format(fpwljsdList.get(i).get("hdje")));
                    hdje += (Double) (fpwljsdList.get(i).get("hdje"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("me")) {
                    sbTable.append(df.format(fpwljsdList.get(i).get("me")));
                    me += (Double) (fpwljsdList.get(i).get("me"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("kdse")) {
                    sbTable.append(df.format(fpwljsdList.get(i).get("kdse")));
                    kdse += (Double) (fpwljsdList.get(i).get("kdse"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("se")) {
                    sbTable.append(df.format(fpwljsdList.get(i).get("se")));
                    se += (Double) (fpwljsdList.get(i).get("se"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("jsje")) {
                    sbTable.append(df.format(fpwljsdList.get(i).get("jsje")));
                    jsje += (Double) (fpwljsdList.get(i).get("jsje"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("ce")) {
                    sbTable.append(df.format(fpwljsdList.get(i).get("ce")));
                    ce += (Double) (fpwljsdList.get(i).get("ce"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpwljsdList.get(i).get("bz")) {
                    sbTable.append(fpwljsdList.get(i).get("bz"));
                }
                sbTable.append("</td>");

                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th>合计：");
            sbTable.append("</th>");

            sbTable.append("<td>");
            sbTable.append(df2.format(jfsl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(je));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df2.format(hdsl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(hdje));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(me));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(kdse));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(se));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(jsje));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(ce));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("</tr>");
            sbTable.append("</table>");
        }
        renderHtml(sbTable.toString());
    }


    @Action("to_fp_xgshsj")
    public String bgjhywk_yq() {
        fp = this.fpService.findfpDetail(fpId);
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        if (fp.getKprq() != null)
            kprq = simple.format(fp.getKprq());
        return SUCCESS;
    }

    @Action("fp_xgshsj")
    public void editCght() {
        try {
            this.fpService.xgkpsj(fp);
            renderText("{\"statusCode\":\"200\", \"message\":\"修改保供合同成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    private Boolean checkFpqxsh(Date kprq) {
        Calendar first = Calendar.getInstance();
        first.add(Calendar.MONTH, 0);//当月
        first.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        Long firstDate = first.getTimeInMillis();
        Calendar end = Calendar.getInstance();
        end.add(Calendar.MONTH, 1);//下个月
        end.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        Long endDate = end.getTimeInMillis();
        if (firstDate < kprq.getTime() && kprq.getTime() < endDate) {
            return true;
        } else {
            return false;
        }
    }
    //==================get and set ================================


    public Long getXsjsdId() {
        return xsjsdId;
    }

    public void setXsjsdId(Long xsjsdId) {
        this.xsjsdId = xsjsdId;
    }

    public Jsd getfp() {
        return fp;
    }

    public void setfp(Jsd fp) {
        this.fp = fp;
    }


    public Jsd getFp() {
        return fp;
    }

    public void setFp(Jsd fp) {
        this.fp = fp;
    }

    public Long getFpId() {
        return fpId;
    }

    public void setFpId(Long fpId) {
        this.fpId = fpId;
    }

    public Long getSprId() {
        return sprId;
    }

    public void setSprId(Long sprId) {
        this.sprId = sprId;
    }

    public Integer getShzt() {
        return shzt;
    }

    public void setShzt(Integer shzt) {
        this.shzt = shzt;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public String getGlqyQc() {
        return glqyQc;
    }

    public void setGlqyQc(String glqyQc) {
        this.glqyQc = glqyQc;
    }

    public Date getCurrentTime() {
        return CurrentTime;
    }

    public void setCurrentTime(Date currentTime) {
        CurrentTime = currentTime;
    }

    public String getGsJc() {
        return gsJc;
    }

    public void setGsJc(String gsJc) {
        this.gsJc = gsJc;
    }

    public Date getSqrqStart() {
        return sqrqStart;
    }

    public void setSqrqStart(Date sqrqStart) {
        this.sqrqStart = sqrqStart;
    }

    public Date getSqrqEnd() {
        return sqrqEnd;
    }

    public void setSqrqEnd(Date sqrqEnd) {
        this.sqrqEnd = sqrqEnd;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getIndex() {
        return index;
    }

    public String getJsdh() {
        return jsdh;
    }

    public Long getGlqyId() {
        return glqyId;
    }

    public void setGlqyId(Long glqyId) {
        this.glqyId = glqyId;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Long getFpzlId() {
        return fpzlId;
    }

    public void setFpzlId(Long fpzlId) {
        this.fpzlId = fpzlId;
    }

    public void setJsdh(String jsdh) {
        this.jsdh = jsdh;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public String getThyy() {
        return thyy;
    }

    public void setThyy(String thyy) {
        this.thyy = thyy;
    }

    public Long getFpcgjsdId() {
        return fpcgjsdId;
    }

    public void setFpcgjsdId(Long fpcgjsdId) {
        this.fpcgjsdId = fpcgjsdId;
    }

    public Long getFpwljsdId() {
        return fpwljsdId;
    }

    public void setFpwljsdId(Long fpwljsdId) {
        this.fpwljsdId = fpwljsdId;
    }

    public Long getFpxsjsdId() {
        return fpxsjsdId;
    }

    public void setFpxsjsdId(Long fpxsjsdId) {
        this.fpxsjsdId = fpxsjsdId;
    }

    public Jsd getCgjsdFp() {
        return cgjsdFp;
    }

    public void setCgjsdFp(Jsd cgjsdFp) {
        this.cgjsdFp = cgjsdFp;
    }

    public Jsd getWljsdFp() {
        return wljsdFp;
    }

    public void setWljsdFp(Jsd wljsdFp) {
        this.wljsdFp = wljsdFp;
    }

    public Jsd getXsjsdFp() {
        return xsjsdFp;
    }

    public void setXsjsdFp(Jsd xsjsdFp) {
        this.xsjsdFp = xsjsdFp;
    }

    public Long getFpzs() {
        return fpzs;
    }

    public void setFpzs(Long fpzs) {
        this.fpzs = fpzs;
    }

    public String getFphm() {
        return fphm;
    }

    public void setFphm(String fphm) {
        this.fphm = fphm;
    }

    public String getFpze() {
        return fpze;
    }

    public void setFpze(String fpze) {
        this.fpze = fpze;
    }

    public String getKprq() {
        return kprq;
    }

    public void setKprq(String kprq) {
        this.kprq = kprq;
    }

    public Integer getZt() {
        return zt;
    }

    public void setZt(Integer zt) {
        this.zt = zt;
    }

    public String getFpIds() {
        return fpIds;
    }

    public void setFpIds(String fpIds) {
        this.fpIds = fpIds;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    @Autowired
    public void setFpService(FpService fpService) {
        this.fpService = fpService;
    }

    @Autowired
    public void setFpDao(FpDao fpDao) {
        this.fpDao = fpDao;
    }

    @Autowired
    public void setCgjsdmxDao(CgjsdmxDao cgjsdmxDao) {
        this.cgjsdmxDao = cgjsdmxDao;
    }

    @Autowired
    public void setXsjsdmxDao(XsjsdmxDao xsjsdmxDao) {
        this.xsjsdmxDao = xsjsdmxDao;
    }

    @Autowired
    public void setWljsdmxDao(WljsdmxDao wljsdmxDao) {
        this.wljsdmxDao = wljsdmxDao;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setCgjsdService(CgjsdService cgjsdService) {
        this.cgjsdService = cgjsdService;
    }

    @Autowired
    public void setWljsdService(WljsdService wljsdService) {
        this.wljsdService = wljsdService;
    }

    @Autowired
    public void setXsjsdService(XsjsdService xsjsdService) {
        this.xsjsdService = xsjsdService;
    }

    @Autowired
    public void setCgdService(CgdService cgdService) {
        this.cgdService = cgdService;
    }

    @Autowired
    public void setXsdService(XsdService xsdService) {
        this.xsdService = xsdService;
    }
}
